﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System;

[Serializable]
public class Map {
    public int LayerIndex = -1;
	public List<Room> roomList;
	public List<KeyValueList> keyValueList;
	public List<ProductCountList> pCountlist;
	public Map(){
		roomList = new List<Room>();
        keyValueList = new List<KeyValueList>();
        pCountlist = new List<ProductCountList>();

        //从1开始是因为 0是ProductType.None
        for (int i = 1; i < Enum.GetNames(typeof(GMConfig.ProductType)).Length; i++)
		{
            keyValueList.Add(new KeyValueList((GMConfig.ProductType)Enum.Parse(typeof(GMConfig.ProductType), i.ToString())));
		}

		//房间物品数量配置
		for (int i = 0; i < Enum.GetNames(typeof(GMConfig.CountRange)).Length; i++)
		{
            pCountlist.Add(new ProductCountList(new ProductCount()));
		}
	}

	public void AddRoom(int level, Room room){
		if (roomList.Contains(room)) return;
		roomList.Add(room);
	}
	public void Clear(){
        roomList.Clear();
        LayerIndex = -1;
	}
    public Map DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream, this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as Map;
        }
    }
}
[Serializable]
public class Room{
	public GMConfig.RoomLevel roomLevel;
    public GMConfig.RoomType roomType;
    public string name;
	public RoomRect minRect;
    public RoomRect maxRect;
	public Room(int level, int type, int minWidth, int minHeight, int maxWidth, int maxHeight){
        roomLevel = (GMConfig.RoomLevel)Enum.Parse(typeof(GMConfig.RoomType), level.ToString());
        roomType = (GMConfig.RoomType)Enum.Parse(typeof(GMConfig.RoomType), type.ToString());
		minRect = new RoomRect(minWidth,minHeight);
		maxRect = new RoomRect(maxWidth,maxHeight);
	}

	public Room(int level, int type, string _name){
        roomLevel = (GMConfig.RoomLevel)Enum.Parse(typeof(GMConfig.RoomType), level.ToString());
        roomType = (GMConfig.RoomType)Enum.Parse(typeof(GMConfig.RoomType), type.ToString());
		name = _name;
	}

	public Room(){}
}
[Serializable]
public class RoomRect{
    [SerializeField]
	public int width;
    [SerializeField]
    public int height;
	public RoomRect(int x, int y){
		width = x;
		height = y;
	}
	public RoomRect(){}
}
[Serializable]
public class KeyValueList{
	public GMConfig.ProductType productType;
	public List<KeyValue> list;
	public KeyValueList(GMConfig.ProductType _productType){
        productType = _productType;
        list = new List<KeyValue>();

    }

	public KeyValueList(){
        list = new List<KeyValue>();
	}
}
[Serializable]
public class KeyValue{
	public string name;
	public int value;
	public int max;
	public KeyValue(string _name, int _value){
		name = _name;
		value = _value;
	}

    public KeyValue(string _name, int _value, int _max)
    {
        name = _name;
        value = _value;
		max = _max;
    }

	public KeyValue(){}
}
[Serializable]
public class ProductCount {
	public int min;
    public int max;

	public ProductCount(int _min, int _max){
		min = _min;
		max = _max;
	}
	public ProductCount(){}
}
[Serializable]
public class ProductCountList{
	public ProductCount productCount;

	public ProductCountList(ProductCount _productCount){
		productCount = _productCount;
	}

	public ProductCountList(){}
}